package com.lp.config;

import com.lp.exception.ApplicationException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

@ControllerAdvice
public class HandlerException {

    @ResponseBody
    @ExceptionHandler(ApplicationException.class)
    public AppReturn applicationException(ApplicationException e) {
        e.printStackTrace();
        //获取自己的异常信息
        if(e.getError() != null) {
            return e.getError();
        }
        return AppReturn.fail(e.getMessage());
    }

    @ResponseBody
    @ExceptionHandler(Exception.class)
    public AppReturn applicationException(Exception e) {
        e.printStackTrace();
        if(e.getMessage() == null || e.getMessage().equals("")) {
            return AppReturn.fail(CodeMsg.FAIL);
        }
        //获取自己的异常信息
        return AppReturn.fail(e.getMessage());
    }

}
